package br.edu.unifei.mestrado.mn;

import java.awt.Paint;

import org.apache.commons.collections15.Transformer;

import br.edu.unifei.mestrado.commons.graph.GraphWrapper;
import br.edu.unifei.mestrado.commons.view.ViewListener;
import br.edu.unifei.mestrado.mem.graph.Graph;
import br.edu.unifei.mestrado.mem.graph.Node;
import br.edu.unifei.mestrado.mem.graph.Relationship;
import br.edu.unifei.mestrado.view.GraphMemPartitioningVisualization;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;

public class GraphViewMem implements ViewListener {

	private boolean showView = true;
	private boolean multiNivel = false;

	private String it = "1";
	private UndirectedSparseGraph<Node, Relationship> graph = null;
	private int cut = -1;

	private String itCoarsed = "1";
	private UndirectedSparseGraph<Node, Relationship> graphCoarsed = null;
	private int cutCoarsed = -1;

	private GraphMemPartitioningVisualization view = null;
	private GraphMemPartitioningVisualization viewCoarsed = null;
	
	private Transformer<Node, String> vertexLabelTransformer;
	private Transformer<Node, Paint> vertexFillPaintTransformer;

	public GraphViewMem() {

	}
	
	public GraphViewMem(boolean multiNivel) {
		this.multiNivel = multiNivel;
	}	

	@Override
	public void initView(GraphWrapper initialGraph) {
		if (showView) {
			view = new GraphMemPartitioningVisualization() {
				private static final long serialVersionUID = 1L;

				@Override
				public String getIteracao() {
					return it;
				}

				@Override
				public edu.uci.ics.jung.graph.Graph<Node, Relationship> getGraph() {
					return graph;
				}

				@Override
				public int getCut() {
					return cut;
				}
			};
			view.setName("1");
			view.setVertexLabelTransformer(vertexLabelTransformer);
			view.setVertexFillPaintTransformer(vertexFillPaintTransformer);

			graph = ((Graph)initialGraph).getGraphToView();
			it = "coarsening";
			cut = -1;
			view.executeView(2);
			
			if(multiNivel) {
				viewCoarsed = new GraphMemPartitioningVisualization() {
					private static final long serialVersionUID = 1L;

					@Override
					public String getIteracao() {
						return itCoarsed;
					}

					@Override
					public edu.uci.ics.jung.graph.Graph<Node, Relationship> getGraph() {
						return graphCoarsed;
					}

					@Override
					public int getCut() {
						return cutCoarsed;
					}
				};
				viewCoarsed.setName("2");
				viewCoarsed.setVertexLabelTransformer(vertexLabelTransformer);
				viewCoarsed.setVertexFillPaintTransformer(vertexFillPaintTransformer);
				
				graphCoarsed = ((Graph)initialGraph).getGraphToView();
				itCoarsed = "coarsening";
				cutCoarsed = -1;
				viewCoarsed.executeView(2);
			}
		}
	}

	@Override
	public void updateView(GraphWrapper grafo, int level, String it, int cutWeight) {
		if (showView) {
			// so para mostrar na tela
			graph = ((Graph)grafo).getGraphToView();
//			cut = part.getCutWeight();
			this.it = it;
			cut = cutWeight;
			view.updateModel();
		}
	}
	
	@Override
	public void updateViewCoarsed(GraphWrapper grafo, int level, String it, int cutWeight) {
		if (showView) {
			if(multiNivel) {
				// so para mostrar na tela
				graphCoarsed = ((Graph)grafo).getGraphToView();
				cutCoarsed = cutWeight;
				this.itCoarsed = it;
				viewCoarsed.updateModel();
			}
		}
	}

	@Override
	public void repaint() {
		if (showView) {
			view.repaint();
			if(multiNivel) {
				viewCoarsed.repaint();
			}
		}
	}

	@Override
	public void setShowView(boolean showView) {
		this.showView = showView;
	}

	public void setVertexLabelTransformer(Transformer<Node, String> vertexLabelTransformer) {
		this.vertexLabelTransformer = vertexLabelTransformer;
	}
	
	public void setVertexFillPaintTransformer(Transformer<Node, Paint> vertexFillPaintTransformer) {
		this.vertexFillPaintTransformer = vertexFillPaintTransformer;
	}
}
